package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacCultureMedium;
import com.ysd.lis.entity.bac.BacWorkFlowNodeDict;
import com.ysd.lis.mapper.bac.BacWorkFlowNodeDictMapper;
import com.ysd.lis.request.BacWorkFlowNodeDictDto;
import com.ysd.lis.service.bac.BacWorkFlowNodeDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-09
 */
@Service
public class BacWorkFlowNodeDictServiceImpl extends ServiceImpl<BacWorkFlowNodeDictMapper, BacWorkFlowNodeDict> implements BacWorkFlowNodeDictService {
    @Autowired
    BacWorkFlowNodeDictMapper bacWorkFlowNodeDictMapper;

    @Override
    public Result findBacWorkFlowNodeDictList(BacWorkFlowNodeDict bacWorkFlowNodeDict) {
        List<BacWorkFlowNodeDict> list = findBacWorkFlowNodeDictArray(bacWorkFlowNodeDict);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacWorkFlowNodeDict> findBacWorkFlowNodeDictArray(BacWorkFlowNodeDict bacWorkFlowNodeDict) {
        MPJLambdaWrapper<BacWorkFlowNodeDict> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacWorkFlowNodeDict::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNodeDict.getId()), BacWorkFlowNodeDict::getId, bacWorkFlowNodeDict.getId());
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNodeDict.getCode()), BacWorkFlowNodeDict::getCode, bacWorkFlowNodeDict.getCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNodeDict.getName()), BacWorkFlowNodeDict::getName, bacWorkFlowNodeDict.getName());
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNodeDict.getNodeTypeNo()), BacWorkFlowNodeDict::getNodeTypeNo, bacWorkFlowNodeDict.getNodeTypeNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNodeDict.getNodeTypeNa()), BacWorkFlowNodeDict::getNodeTypeNa, bacWorkFlowNodeDict.getNodeTypeNa());
        queryWrapper.orderByAsc(BacWorkFlowNodeDict::getSeq);
        return bacWorkFlowNodeDictMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacWorkFlowNodeDict(BacWorkFlowNodeDict bacWorkFlowNodeDict) {
        int i = bacWorkFlowNodeDictMapper.insert(bacWorkFlowNodeDict);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacWorkFlowNodeDict);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacWorkFlowNodeDict(BacWorkFlowNodeDictDto bacWorkFlowNodeDictDto) {
        List<BacWorkFlowNodeDict> bacCultureMedia = bacWorkFlowNodeDictDto.getBacWorkFlowNodeDicts();

        List<String> ids = bacCultureMedia.stream().map(BacWorkFlowNodeDict::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBacWorkFlowNodeDict(BacWorkFlowNodeDict bacWorkFlowNodeDict) {
        int i = bacWorkFlowNodeDictMapper.updateById(bacWorkFlowNodeDict);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacWorkFlowNodeDict);
        }
        return Result.fail("更新失败");
    }
}
